package com.shujia.day12;



import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

public class Demo09IOReadLine {
    public static void main(String[] args) throws IOException {
        /*
           TODO  按行读取一行数据
                使用字节数组的方式获取所有的数据，并截取出一行数据
                整个过程太麻烦了...
                于是寻找读取一行的方法
         */
        FileReader fileReader = new FileReader("data/students.txt");
        char[] chars = new char[10];
        int readLen = 0;
        String oneLine = "";
        ArrayList<String> allLineStr = new ArrayList<>();
        while ((readLen = fileReader.read(chars)) > 0) {
            String emp = new String(chars, 0, readLen);
            // 当获取到的数据是完整的一行时，我们将之前的所有数据都赋予给 oneLine
            boolean contains = emp.contains("\n");
            if (contains){
                String[] split = emp.split("\n");
                String lastLine = split[0];
                String nextLine = "";
                try {
                    nextLine = split[1];
                }catch (ArrayIndexOutOfBoundsException exception){
                    exception.printStackTrace();
                }
                oneLine += lastLine;
                allLineStr.add(oneLine);
                oneLine = "";
                oneLine += nextLine;
            }else {
                oneLine += emp;
            }

//            System.out.println(emp + "是否换行:"+contains);
        }
        for (String line : allLineStr) {
            System.out.println(line);
        }

        fileReader.close();

    }
}
